<template>
  <ContentWrap>
    <!--    顶部查询项-->
    <el-form :model="form" inline label-width="70px">
      <el-form-item :label="t('modelInfo.model_name')">
        <el-input
          v-model="form.model_name"
          clearable
          :placeholder="t('modelInfo.model_name')"
          style="width: 250px"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="initialize">查询</el-button>
        <el-button icon="Refresh" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <!--    操作项-->
    <div class="flex flex-items-center my-5">
      <el-button type="primary" @click="handleAdd('add')">
        {{ t('modelInfo.add') }}
      </el-button>
    </div>
    <!-- 表格主体 -->
    <el-table
      :data="dataList"
      highlight-current-row
      stripe
      border
      v-loading="tableLoading"
      :cell-style="{ textAlign: 'center' }"
      :header-cell-style="{ textAlign: 'center' }"
    >
      <el-table-column :label="t('expandCustomer.serialNumber')" prop="id" min-width="70" />
      <el-table-column
        :label="t('modelInfo.model_name')"
        min-width="200"
        prop="model_name"
        :show-overflow-tooltip="true"
      />
      <el-table-column :label="t('modelInfo.latest_firmware_version')" min-width="200">
        <template #default="{ row }">
          <el-link type="primary" :href="row.firmware_url" target="_blank">
            {{ row.latest_firmware_version }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column
        :label="t('modelInfo.operator')"
        min-width="200"
        prop="operator_name"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="t('modelInfo.create_time')"
        min-width="200"
        prop="create_time"
        :show-overflow-tooltip="true"
      />
      <el-table-column fixed="right" label="操作" width="200">
        <template #default="{ row }">
          <el-button @click="handleJump('edit', row)" size="small">
            {{ t('modelInfo.edit') }}
          </el-button>
          <el-button type="success" @click="handleJump('detail', row)" size="small">
            {{ t('modelInfo.detail') }}
          </el-button>
          <!--          <el-button @click="handleDelete('single', row)" size="small" type="danger">-->
          <!--            {{ t('system.marketChannel.delete') }}-->
          <!--          </el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <!--    分页-->
    <Pagination
      :total="paginationTotal"
      v-model:page="form.pageIndex"
      v-model:limit="form.pageSize"
      @pagination="initialize"
    />
  </ContentWrap>
</template>

<script setup lang="ts">
import Pagination from '@/components/Pagination/index.vue'
import { ContentWrap } from '@/components/ContentWrap'
import { getListApi } from '@/api/modelInfo'
import { ElButton } from 'element-plus'

const { t } = useI18n()
const { push } = useRouter()

const tableLoading = ref<boolean>(false)
const dataList = ref<any[]>([])
const paginationTotal = ref<number>(0)

const form = ref<Record<string, any>>({
  model_name: '',
  pageIndex: 1,
  pageSize: 15
})

//点击跳转到新增
const handleAdd = (type: string) => {
  push({ path: `/channelManage/modelManagement/${type}` })
}

//初始化请求
const initialize = async () => {
  tableLoading.value = true
  const params = JSON.parse(JSON.stringify(form.value))
  delete params.date
  const resp = await getListApi(params)
  dataList.value = resp.data.list
  paginationTotal.value = resp.data.total
  tableLoading.value = false
}

const handleJump = (type: string, row?: Record<string, any>) => {
  push({
    path: `/channelManage/modelManagement/${type}`,
    query: { [(row && 'id') as string]: row?.id }
  })
}

//重置查询项
const reset = () => {
  form.value = {
    model_name: '',
    pageIndex: 1,
    pageSize: 15
  }
  paginationTotal.value = 0
  initialize()
}

onMounted(() => {
  initialize()
})
</script>

<style lang="less" scoped></style>
